////////////////////////////////////////////////////////////////////////////////
// Filename: L_window.cpp
////////////////////////////////////////////////////////////////////////////////
#include "L_window.h"

L_Window::L_Window()
{

}

L_Window::~L_Window()
{

}

bool L_Window::Init(HINSTANCE hInstance, int ShowWnd, int width, int height, string Title)
{
	WNDCLASSEX wc;

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = NULL;
	wc.cbWndExtra = NULL;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "Window class";
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Error registering class",	"Error", MB_OK | MB_ICONERROR);
		return 1;
	}

	RECT rect = {50,50,50+width, 50+height};
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);

	Hwnd = CreateWindowEx(	NULL, "Window class", Title.c_str(),
							WS_OVERLAPPEDWINDOW,
							rect.left, rect.top, rect.right-rect.left, rect.bottom - rect.top,
							NULL, NULL, hInstance, NULL );

	if (!Hwnd)
	{
		MessageBox(NULL, "Error creating window", "Error", MB_OK | MB_ICONERROR);
		return 1;
	}

	ShowWindow(Hwnd, ShowWnd);
	UpdateWindow(Hwnd);

	return true;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_KEYDOWN:
		switch(wParam)
		{
		case VK_ESCAPE:
			DestroyWindow(hwnd);
			return 0;
		}
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

HWND L_Window::GetHwnd()
{
	return Hwnd;
}